﻿using Md.Api.Entity.ModelDB;
using PetaPoco.NetCore;

namespace Md.Petapoco.Repositories.MdTrainingEducation;
/// <summary>
/// 系统角色仓储
/// </summary>
public class SysRoleRep : BaseRep<SysRoleModel>
{
    /// <summary>
    /// 通过角色编号获取系统角色信息
    /// </summary>
    /// <param name="roleId">角色编号</param>
    /// <returns>SysRoleModel</returns>
    public SysRoleModel GetSysRoleByRoleId(long roleId)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select `role_id`
                  ,`role_name`
                  ,`role_key`
                  ,`role_sort`
                  ,`data_scope`
                  ,`menu_check_strictly`
                  ,`dept_check_strictly`
                  ,`status`
                  ,`del_flag`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_role`
            where 1 = 1 and `role_id` = @0
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<SysRoleModel>(sqlStr, roleId);
    }
    /// <summary>
    /// 获取系统角色列表数据
    /// </summary>
    /// <param name="model">筛选条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="beginTime">开始时间</param>
    /// <param name="endTime">结束时间</param>
    /// <returns>IEnumerable<SysRoleModel></returns>
    public IEnumerable<SysRoleModel> GetSysRoleList(SysRoleModel model, string? orderBy, bool? isDesc, DateTime? beginTime, DateTime? endTime)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.RoleId > 0)
        {
            sqlWhere += " and role_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.RoleName))
        {
            sqlWhere += " and role_name like @1 ";
        }
        if (!string.IsNullOrWhiteSpace(model.RoleKey))
        {
            sqlWhere += " and role_key  @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DataScope))
        {
            sqlWhere += " and data_scope = @3 ";
        }
        if (model.MenuCheckStrictly > 0)
        {
            sqlWhere += " and menu_check_strictly = @4 ";
        }
        if (model.DeptCheckStrictly > 0)
        {
            sqlWhere += " and dept_check_strictly = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @6 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @7 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @8 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @9 ";
        }
        if (beginTime.HasValue)
        {
            sqlWhere += " and create_time >= @10 ";
        }
        if (endTime.HasValue)
        {
            sqlWhere += " and create_time <= @11 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `role_id`
                  ,`role_name`
                  ,`role_key`
                  ,`role_sort`
                  ,`data_scope`
                  ,`menu_check_strictly`
                  ,`dept_check_strictly`
                  ,`status`
                  ,`del_flag`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_role`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Query<SysRoleModel>(
            sqlStr,
            model.RoleId,
            string.Format(@"%{0}%", model.RoleName),
            string.Format(@"%{0}%", model.RoleKey),
            model.DataScope,
            model.MenuCheckStrictly,
            model.DeptCheckStrictly,
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy,
            string.Format(@"{0} 00:00:00", beginTime),
            string.Format(@"{0} 23:59:59", endTime));
    }
    /// <summary>
    /// 分页获取系统角色列表数据
    /// </summary>
    /// <param name="model">筛选条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="beginTime">开始时间</param>
    /// <param name="endTime">结束时间</param>

    /// <returns>Page<SysRoleModel></returns>
    public Page<SysRoleModel> GetSysRolePageList(SysRoleModel model, int pageIndex, int pageSize, string? orderBy, bool? isDesc, DateTime? beginTime, DateTime? endTime)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.RoleId > 0)
        {
            sqlWhere += " and role_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.RoleName))
        {
            sqlWhere += " and role_name like @1 ";
        }
        if (!string.IsNullOrWhiteSpace(model.RoleKey))
        {
            sqlWhere += " and role_key  @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DataScope))
        {
            sqlWhere += " and data_scope = @3 ";
        }
        if (model.MenuCheckStrictly > 0)
        {
            sqlWhere += " and menu_check_strictly = @4 ";
        }
        if (model.DeptCheckStrictly > 0)
        {
            sqlWhere += " and dept_check_strictly = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @6 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @7 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @8 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @9 ";
        }
        if (beginTime.HasValue)
        {
            sqlWhere += " and create_time >= @10 ";
        }
        if (endTime.HasValue)
        {
            sqlWhere += " and create_time <= @11 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `role_id`
                  ,`role_name`
                  ,`role_key`
                  ,`role_sort`
                  ,`data_scope`
                  ,`menu_check_strictly`
                  ,`dept_check_strictly`
                  ,`status`
                  ,`del_flag`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_role`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Page<SysRoleModel>(
            pageIndex,
            pageSize,
            sqlStr,
            model.RoleId,
            string.Format(@"%{0}%", model.RoleName),
            string.Format(@"%{0}%", model.RoleKey),
            model.DataScope,
            model.MenuCheckStrictly,
            model.DeptCheckStrictly,
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy,
            string.Format(@"{0} 00:00:00", beginTime),
            string.Format(@"{0} 23:59:59", endTime));
    }
    /// <summary>
    /// 通过角色编号更新系统角色
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysRoleByRoleId(SysRoleModel model)
    {
        var sql = new Sql();

        sql.Append(" update `sys_role` set ");

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.RoleName))
        {
            sql.Append(" role_name = @0, ", model.RoleName);
        }

        if (!string.IsNullOrWhiteSpace(model.RoleKey))
        {
            sql.Append(" role_key = @0, ", model.RoleKey);
        }

        if (model.RoleSort > -1)
        {
            sql.Append(" role_sort = @0, ", model.RoleSort);
        }

        if (!string.IsNullOrWhiteSpace(model.DataScope))
        {
            sql.Append(" data_scope = @0, ", model.DataScope);
        }

        if (model.MenuCheckStrictly > -1)
        {
            sql.Append(" menu_check_strictly = @0, ", model.MenuCheckStrictly);
        }

        if (model.DeptCheckStrictly > -1)
        {
            sql.Append(" dept_check_strictly = @0, ", model.DeptCheckStrictly);
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sql.Append(" status = @0, ", model.Status);
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sql.Append(" update_by = @0, ", model.UpdateBy);
        }

        if (!string.IsNullOrWhiteSpace(model.Remark))
        {
            sql.Append(" remark = @0, ", model.Remark);
        }
        
        sql.Append(" update_time = sysdate() ");
        #endregion

        sql.Append(" where 1 = 1 and del_flag = @0 and role_id = @1 ", model.DelFlag, model.RoleId);

        return MdTrainingEducationDB.GetInstance().Execute(sql);
    }
}

